package cn.yj.core.business.service;

import cn.yj.annotation.pagehelper.page.Page;
import cn.yj.cgs.dba.BaseEntity;
import cn.yj.cgs.dba.entity.UserAddress;
import cn.yj.cgs.dba.mapper.UserAddressMapper;
import cn.yj.commons.utils.CheckUtils;
import cn.yj.commons.utils.StringUtils;
import cn.yj.core.business.service.iservice.IUserAddressService;
import cn.yj.params.check.annotation.CheckObjectValue;
import cn.yj.params.check.annotation.KeyValue;
import cn.yj.params.check.annotation.Require;
import cn.yj.tools.exception.ServiceException;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author 永健
 * @since 2021-05-09 14:07
 */
@Service
public class UserAddressService extends ServiceImpl<UserAddressMapper, UserAddress> implements IUserAddressService {

    @Override
    public Page<UserAddress> pageList(Map<String, Object> prams, Page<UserAddress> page) {
        baseMapper.findList(prams, page);
        return page;
    }

    @Override
    public boolean exitsById(Integer id) {
        return count(queryLambda().eq(BaseEntity::getId, id)) > 0;
    }

    @CheckObjectValue(keyValue = @KeyValue(type = UserAddress.class, name = {"name", "phone", "province", "city",
            "district", "address", "isDefault", "userCode"}))
    @Override
    public boolean addAddress(UserAddress userAddress) {
        if (!CheckUtils.isPhoneLegal(StringUtils.trimAll(userAddress.getPhone()))){
            throw new ServiceException("请输入正确的手机号码");
        }
        // 如果是默认的 将原来的默认改为非默认
        updateDefaultToFalse();

        return super.save(userAddress);
    }

    @CheckObjectValue(keyValue = @KeyValue(type = UserAddress.class, name = {"id", "name", "phone", "province", "city"
            , "district", "address", "isDefault"}))
    @Override
    public boolean updateAddress(UserAddress userAddress) {
        CheckUtils.isPhoneLegal(StringUtils.trimAll(userAddress.getPhone()));
        if (userAddress.getIsDefault()) {
            // 如果是默认的 将原来的默认改为非默认
            updateDefaultToFalse();
        }
        return super.updateById(userAddress);
    }

    @Override
    public boolean updateDefault(@Require Integer id, @Require Boolean isDefault) {
        UserAddress userAddress = getById(id);

        if (StringUtils.isNull(userAddress)) {
            log.error("user addressId:" + id);
            throw new ServiceException("找不到改地址信息");
        }

        if (userAddress.getIsDefault().equals(isDefault)) {
            return true;
        }
        updateDefaultToFalse();

        userAddress.setId(id);
        userAddress.setIsDefault(isDefault);
        return super.updateById(userAddress);
    }

    @Override
    public UserAddress getDefaultAddr(@Require String userCode) {
        return getOne(queryLambda().eq(UserAddress::getIsDefault, true).eq(UserAddress::getUserCode, userCode));
    }

    private void updateDefaultToFalse() {
        UserAddress address = getOne(queryLambda().eq(UserAddress::getIsDefault, true));
        if (StringUtils.isNotNull(address)) {
            address.setIsDefault(false);
            super.updateById(address);
        }
    }
}
